package com.qiwx.pigeon.activity.stock;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qiwx.pigeon.R;
import com.qiwx.pigeon.Utils.MyToast;
import com.qiwx.pigeon.Utils.StringUtil;
import com.qiwx.pigeon.app.AppContext;
import com.qiwx.pigeon.base.BaseMVPActivity;
import com.qiwx.pigeon.data.network.BaseResponse;
import com.qiwx.pigeon.data.vo.pageinfo.PageInfo;
import com.qiwx.pigeon.data.vo.stock.CustomerBean;
import com.qiwx.pigeon.data.vo.stock.SupplierBean;
import com.qiwx.pigeon.presenter.CustomerPresenter;
import com.qiwx.pigeon.presenter.SupplierPresenter;
import com.qiwx.pigeon.viewinterface.ICommonView;
import com.qiwx.pigeon.widget.ArrowView;

import java.lang.ref.WeakReference;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @description
 * @Author qiwx
 * @Date 2020/9/2 5:00 PM
 * 添加客户
 **/
public class AddCustomerActivity extends BaseMVPActivity<ICommonView<CustomerBean>, CustomerPresenter> implements ICommonView<CustomerBean> {
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.title_return)
    ArrowView titleReturn;
    @BindView(R.id.title_right)
    TextView titleRight;
    @BindView(R.id.llTitle)
    LinearLayout llTitle;
    @BindView(R.id.edt_company_name)
    EditText edtCompanyName;
    @BindView(R.id.edt_company_adress)
    EditText edtCompanyAdress;
    @BindView(R.id.edt_company_phone)
    EditText edtCompanyPhone;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    String companyName;
    String companyAdress;
    String companyPhone;


    @Override
    public CustomerPresenter createPresenter() {
        return new CustomerPresenter(new WeakReference(this));
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_add_customer_layout;
    }

    @Override
    public void initAfterOnCreate() {
        initView();
    }

    private void initView() {
        titleReturn.changeArrowLeft();
        titleContent.setText("添加客户");
    }

    public boolean checkParams() {
        companyName = edtCompanyName.getText().toString();
        companyAdress = edtCompanyAdress.getText().toString();
        companyPhone = edtCompanyPhone.getText().toString();
        if (StringUtil.isEmpty(companyName)) {
            MyToast.showCenterShort("客户名称不能为空");
            return false;
        }
        if (StringUtil.isEmpty(companyAdress)) {
            MyToast.showCenterShort("客户地址不能为空");
            return false;
        }
        if (StringUtil.isEmpty(companyPhone)) {
            MyToast.showCenterShort("客户电话不能为空");
            return false;
        }
        return true;
    }

    private void submitData() {
        CustomerBean bean = new CustomerBean();
        bean.setF_id(AppContext.getFarmId(context));
        bean.setAdress(companyAdress);
        bean.setName(companyName);
        bean.setTelephone(companyPhone);
        mPresenter.addCustomer(bean);

    }


    @OnClick({R.id.title_return, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_return:
                finish();
                break;
            case R.id.btn_submit:
                if (checkParams()) {
                    submitData();
                }
                break;
        }
    }

    @Override
    public void onAddSuccess(BaseResponse response) {
        MyToast.showCenterShort("添加成功");
        finish();
//        edtCompanyName.setText("");
//        edtCompanyAdress.setText("");
//        edtCompanyPhone.setText("");

    }

    @Override
    public void onDeleteSuccess(BaseResponse response) {

    }

    @Override
    public void onQueryListSuccess(PageInfo<CustomerBean> list) {

    }
}
